<?php
namespace app\servercertificate\logic;
use \app\manage\logic\BaseLogic;
use \app\manage\service\ApiBase;
/**
 * @author:      李颖
 * @DateTime:    2017-07-10 15:29:30
 * @Description: 证书
 */
class Certificate extends BaseLogic
{
	
	protected $certificateLogic = 'app\servercertificate\logic\Certificate';
    protected $certificateValidate = 'app\servercertificate\validate\Certificate';

    use \app\serveruser\traits\RunOneAddUserInfo;
    
	
	//获取证书详情
	public function certificateInfo($data): array 
	{
		return $this->_runOneAddOneUserInfo($data, 'servercertificate/certificate/Certificate/certificateInfo', $this->certificateValidate, $this->certificateLogic);

	}
	
	//后台获取不同状态下的证书列表
	public function adminAllCertiList($data): array 
	{
		return $this->_runOneAddLotsUserInfo($data, 'servercertificate/certificate/Certificate/adminAllCertiList', $this->certificateValidate, $this->certificateLogic);
	}

	/*后台管理员对某证书进行发货操作*/
	/*流程如下：
		1、解冻用户资金
		2、用户资金转账平台
		3、平台进行发货操作
	*/
	public function shipmentsGoods($data): array 
	{
        //1、获取证书提货信息(提货信息中有订单付款信息)
        $getPickInfo['certificate_id'] = $data['certificate_id'];
        $getPick['query'] = json_encode([
            ['path'=>'servercertificate/certificate/Pickupgoods/adminOnePickUpInfo', 'param'=>$getPickInfo]
        ]);
        $getPickRes = ApiBase::sendDataOne(config('proxy_queue_url'), $getPick);
        if($getPickRes['status'] != SUCCESS_STATUS){ return verify_err(null, '该证书没有提货信息'); }
        if($getPickRes['res']['certificate_is_pick'] == 1){  return verify_err(null, '该证书已提货成功,不可再提'); }
        if($getPickRes['res']['transfer_status'] == 1){  return verify_err(null, '后台已对该证书进行了发货操作'); }
        $orderInfo = $getPickRes['res']['certificate_order_info'];
        //2、获取申请提货人信息与app_id
        $app_key = ApiBase::getAppKey();
        $getBaseInfo['query'] = json_encode([
            ['path'=>'serveruser/user/User/info', 'param'=>['id'=>$orderInfo['user_id']]],
            ['path'=>'serveruser/auth/App/find', 'param'=>['app_key'=>$app_key]],
            ['path'=>'serveruser/auth/Token/hGetAll', 'param'=>['token'=>$data['token']]]
        ]);
        $baseInfo = ApiBase::sendDataLots(config('proxy_all_url'), $getBaseInfo);
        if($baseInfo['status'] != SUCCESS_STATUS){
            return $baseInfo;
        }

        //3、调用接口 “解冻用户资金并转账给平台”
        $affirmPickUpGoods['user_id'] = $orderInfo['user_id'];
        $affirmPickUpGoods['opposite_id'] = YIHUO_ID;
        $affirmPickUpGoods['certificate_price'] = $getPickRes['res']['certificate_price'];
        $affirmPickUpGoods['update_ip'] = request()->ip();
        $affirmPickUpGoods['app_id'] = $baseInfo['res'][1]['id'];
        $affirmPickUpGoods['user_info'] = $baseInfo['res'][0];
        $affirmPickUpGoods['opposite_info'] = $baseInfo['res'][2];
        $affirmPickUpGoods['type'] = 'order';
        $affirmPickUpGoods['order_id'] = $orderInfo['id'];
        $affirmPickUpGoods['pay_method'] = $getPickRes['res']['pay_method'];
        $pickUpData['query'] = json_encode([
            ['path'=>'serverfund/fund/FundAccount/affirmPickUpGoods', 'param'=>$affirmPickUpGoods]
        ]);
        $pickUpRes = ApiBase::sendDataOne(config('proxy_queue_url'), $pickUpData);
        if($pickUpRes['status'] != SUCCESS_STATUS){
            return $pickUpRes;
        }
        //4、平台进行发货操作
        $shipmentsGoodsQuery['query'] = json_encode([
            ['path'=>'servercertificate/certificate/Pickupgoods/shipmentsGoods', 'param'=>$data]
        ]);
        return ApiBase::sendDataOne(config('proxy_queue_url'), $shipmentsGoodsQuery);
	}

	

}